package org.xfg.domain.active.service.partake;

import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.xfg.domain.active.model.aggregate.CreatePartakeOrderAggregate;
import org.xfg.domain.active.model.entity.ActivityEntity;
import org.xfg.domain.active.model.entity.PartakeRaffleActivityEntity;
import org.xfg.domain.active.model.entity.UserRaffleOrderEntity;
import org.xfg.domain.active.model.valobj.ActivityStateVO;
import org.xfg.domain.active.repository.IActivityRepository;
import org.xfg.domain.active.service.IRaffleActivityPartakeService;
import org.xfg.types.enums.ResponseCode;
import org.xfg.types.exception.AppException;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
public abstract class AbstractRaffleActivityPartake implements IRaffleActivityPartakeService {

    @Resource
    protected IActivityRepository activityRepository;

    @Override
    public UserRaffleOrderEntity createOrder(String userId,Long activityId) {
        PartakeRaffleActivityEntity req = PartakeRaffleActivityEntity.builder()
                .userId(userId)
                .activityId(activityId)
                .build();
        return createOrder(req);
    }

    // 参与抽奖创建抽奖单
    @Override
    public UserRaffleOrderEntity createOrder(PartakeRaffleActivityEntity partakeRaffleActivityEntity) {
        String userId = partakeRaffleActivityEntity.getUserId();
        Long activityId = partakeRaffleActivityEntity.getActivityId();
        Date currentDate = new Date();

        // 1.活动查询实体
        ActivityEntity activityEntity = activityRepository.queryRaffleActivityByActivityId(activityId);

        // 校验活动状态
        if (!ActivityStateVO.open.equals(activityEntity.getState())){
            throw new AppException(ResponseCode.ACTIVITY_STATE_ERROR.getCode(),ResponseCode.ACTIVITY_STATE_ERROR.getInfo());
        }
        // 检验活动日期
        if (activityEntity.getBeginDateTime().after(currentDate) || activityEntity.getEndDateTime().before(currentDate)){
            throw new AppException(ResponseCode.ACTIVITY_DATE_ERROR.getCode(),ResponseCode.ACTIVITY_DATE_ERROR.getInfo());
        }

        //2. 查询未使用的活动参与订单记录
        UserRaffleOrderEntity userRaffleOrderEntity = activityRepository.queryNoUsedRaffleOrder(partakeRaffleActivityEntity);
        if (null != userRaffleOrderEntity) {
            log.info("创建参与活动订单 userId:{} activityId:{} userRaffleOrderEntity:{}", userId, activityId, JSON.toJSONString(userRaffleOrderEntity));
            return userRaffleOrderEntity;
        }

        // 3. 额度账户过滤&返回账户构建对象
        CreatePartakeOrderAggregate createPartakeOrderAggregate = this.doFilterAccount(userId, activityId, currentDate);

        // 4. 构建用户抽奖订单实体
        UserRaffleOrderEntity userRaffleOrder = this.buildUserRaffleOrder(userId, activityId, currentDate);

        // 5. 填充抽奖实体对象
        createPartakeOrderAggregate.setUserRaffleOrderEntity(userRaffleOrder);

        // 6. 保存聚合对象 一个领域内一个聚合是一个事务
        activityRepository.saveCreatePartakeOrderAggregate(createPartakeOrderAggregate);

        return userRaffleOrder;

    }

    protected abstract CreatePartakeOrderAggregate doFilterAccount(String userId, Long activityId, Date currentDate) ;

    protected abstract UserRaffleOrderEntity buildUserRaffleOrder(String userId, Long activityId, Date currentDate);


}
























